package com.ruoyi.base.service;

import com.ruoyi.base.domain.BaseCate;
import com.ruoyi.base.domain.BasePro;
import com.ruoyi.base.domain.vo.ProVO;
import com.ruoyi.base.mapper.BaseCateMapper;
import com.ruoyi.base.mapper.BaseProMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BaseProService {

    @Autowired
    private BaseProMapper baseProMapper;

    @Autowired
    private BaseCateMapper baseCateMapper;

    public void saveBasePro(BasePro basePro) {
        baseProMapper.insert(basePro);
    }

    public List<BasePro> list(ProVO vo) {
        List<BasePro> basePros = baseProMapper.selectList(vo);
        Map<Integer, BaseCate> buffer = new HashMap<>();
        for (BasePro basePro : basePros) {
            Integer code = basePro.getCategoryCode();
            BaseCate baseCate = buffer.get(code);
            if (baseCate == null) {
                baseCate = baseCateMapper.selectById(code);
                buffer.put(code, baseCate);
            }
            basePro.setBaseCate(baseCate);
        }

        return basePros;
    }
}
